﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.Reports;
namespace ERP.Hoadon
{
    public partial class viewERP_Hoadon_Thanhtoan : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
        public cs_erp_khachhang_sodoInfo hh = new cs_erp_khachhang_sodoInfo();

        public cs_erp_hanghoaInfo hanghoa = new cs_erp_hanghoaInfo();

        int count = 0;
        public int flag = 1;

        public viewERP_Hoadon_Thanhtoan()
        {
            InitializeComponent();
        }

        private void viewERP_Hoadon_Thanhtoan_Load(object sender, EventArgs e)
        {
            this.Load_Info();
            this.Load_Combo();
            this.CheckUser();
        }
        private void Load_Combo()
        {
            DataSet ds = new DataSet("ds");
            //this.hanghoa.Get_List = this.kh.id_kh;
            cs_erp_ht_ttInfo httt = new cs_erp_ht_ttInfo();
            ds = httt.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                //this.count = 0;
            }
            else
            {
                //this.cboKhonhap.Properties.DataSource = ds.Tables[0];
                this.cboHttt.Properties.DataSource = ds.Tables[0];
            }
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void Load_Info()
        {
            this.kh.id_kh = this.hoadon.id_kh;
            this.kh.Get_Info();
            this.txtHoten.Text = this.kh.tenkhachhang;
            this.txtDiachi.Text = this.kh.diachi;
            this.txtDienthoai.Text = this.kh.dienthoai;
            this.txtHoadon.Text = this.hoadon.sohd;
            this.txtNgayban.EditValue = this.hoadon.ngayban;
            this.txtNgaythu.EditValue = this.hoadon.ngayhentra;
            this.txtNgaythanhtoan.EditValue = DateTime.Now;
            this.txtTongtien.EditValue = Convert.ToDecimal(this.hoadon.tongtien);
            this.txtKhuyenmai.EditValue = Convert.ToDecimal(this.hoadon.khuyenmai);
            this.txtDathanhtoan.EditValue = Convert.ToDecimal(this.hoadon.datra);
            this.txtPhaithanhtoan.EditValue = Convert.ToDecimal(this.hoadon.conno);
            this.txtThanhtoan.EditValue = Convert.ToDecimal(this.hoadon.conno);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            cs_erp_hoadon_thanhtoanInfo tt = new cs_erp_hoadon_thanhtoanInfo();
            tt.id_hoadon = this.hoadon.id_hd;
            tt.ngaythanhtoan = Convert.ToDateTime(this.txtNgaythanhtoan.EditValue);
            tt.nguoinhantien = this.user.id_nv;
            tt.lanthanhtoan = 1;
            tt.sotien = Convert.ToDouble(this.txtThanhtoan.Value.ToString().Trim());
            tt.ghichu = this.txtGhichu.Text;
            try
            {
                tt.httt = Convert.ToInt32(this.cboHttt.EditValue.ToString().Trim());
            }
            catch
            {
                XtraMessageBox.Show("Bạn phải chọn Hình thức thanh toán của Khách hàng!", "Thông báo");
                return;
            }
            if (tt.sotien <= 0)
            {
                XtraMessageBox.Show("Số tiền thanh toán phải > 0!", "Thông báo");
                return;
            }
            tt.ExcuteProcedure(1);
            XtraMessageBox.Show("Lưu thanh toán Hóa đơn thành công!", "Thông báo");
            this.inHoadon(tt);
            this.Close();
        }

        private void inHoadon(cs_erp_hoadon_thanhtoanInfo tt)
        {
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.captren = "0";
            dv.Get_Info_by_Parents();
            rptPhieuthu rpt = new rptPhieuthu();
            rpt.lbDonvi.Text = dv.tendonvi;
            rpt.lbDiachi.Text = dv.diachi;
            rpt.lblDienthoai.Text = dv.sodt_fax;
            rpt.lblHoten.Text = this.kh.tenkhachhang;
            rpt.lblDiachi.Text = this.kh.diachi;
            rpt.lblSotien.Text = tt.sotien.ToString();
            rpt.lblBangchu.Text = this.DocTienChu(tt.sotien);
            rpt.lblBangchu2.Text = this.DocTienChu(tt.sotien);
            rpt.lblSohd.Text = this.hoadon.sohd + "-" + tt.id_lanthanhtoan.ToString().Trim();
            rpt.ShowPreview();

        }
        public string DocTienChu(double sotien)
        {
            if (sotien == 0)
            {
                return "không đồng";
            }



            string[] chuso = new string[10];
            chuso[0] = "không";
            chuso[1] = "một";
            chuso[2] = "hai";
            chuso[3] = "ba";
            chuso[4] = "bốn";
            chuso[5] = "năm";
            chuso[6] = "sáu";
            chuso[7] = "bảy";
            chuso[8] = "tám";
            chuso[9] = "chín";

            string[] donvi = new string[5];
            donvi[0] = "";
            donvi[1] = "nghìn";
            donvi[2] = "triệu";
            donvi[3] = "tỷ";

            int tien;
            tien = Convert.ToInt32(sotien);

            string tienchu;
            tienchu = "";
            if (tien < 0)
            {
                tienchu = "âm ";
                tien = -tien;
            }
            int[] mangso = new int[20];
            int Xmod;
            int count = 0;
            //int Xdiv;
            while (tien > 0)
            {
                Xmod = tien % 10;
                tien = tien / 10;
                mangso[count] = Xmod;
                count++;
            }
            int dem = 0;

            //Khoi tao mang a
            int[,] a = new int[10, 3];
            for (int j = 0; j < 10; j++)
            {
                for (int jj = 0; jj < 3; jj++)
                {
                    a[j, jj] = 10;
                }
            }
            while (count % 3 != 0)
            {
                count++;
                mangso[count] = 10;
            }
            //Dua mangso vao mang a
            for (int k = 0; k <= count; k++)
            {
                dem = k / 3;
                a[dem, k % 3] = mangso[k];
                //MessageBox.Show(a[dem,k%3].ToString());

            }
            //bat dau tinh
            string tam = "";

            for (int i = dem - 1; i >= 0; i--)
            {
                if (a[i, 2] == 0 && a[i, 1] == 0 && a[i, 0] == 0)
                {
                    tam = "";
                }
                else
                    if (a[i, 2] == 0 || a[i, 2] == 10)
                    {
                        if (a[i, 1] > 1 && a[i, 1] < 10)
                        {
                            if (a[i, 0] == 0)
                            {
                                tam = chuso[a[i, 1]] + " mươi ";
                            }
                            else
                            {
                                tam = chuso[a[i, 1]] + " mươi " + chuso[a[i, 0]];
                            }
                        }
                        else
                            if (a[i, 1] == 1)
                            {
                                if (a[i, 0] == 0)
                                {
                                    tam = " mười ";
                                }
                                else
                                {
                                    tam = " mười " + chuso[a[i, 0]];
                                }
                            }
                            else if (a[i, 1] == 0)
                            {
                                if (a[i, 0] == 0)
                                {
                                    tam = "";
                                }
                                else
                                {
                                    if (i != dem - 1)
                                    {
                                        tam = " lẻ " + chuso[a[i, 0]];
                                    }
                                    else
                                    {
                                        tam = chuso[a[i, 0]];
                                    }
                                }
                            }
                    }
                    else
                    {
                        //tam=chuso[a[i,2]] + " trăm " ;
                        if (a[i, 1] > 1 && a[i, 1] < 10)
                        {
                            if (a[i, 0] == 0)
                            {
                                tam = chuso[a[i, 1]] + " mươi ";
                            }
                            else
                            {
                                tam = chuso[a[i, 1]] + " mươi " + chuso[a[i, 0]];
                            }
                        }
                        else
                            if (a[i, 1] == 1)
                            {
                                if (a[i, 0] == 0)
                                {
                                    tam = " mười ";
                                }
                                else
                                {
                                    tam = " mười " + chuso[a[i, 0]];
                                }
                            }
                            else if (a[i, 1] == 0)
                            {
                                if (a[i, 0] == 0)
                                {
                                    tam = "";
                                }
                                else
                                {
                                    tam = " lẻ " + chuso[a[i, 0]];
                                }
                            }
                        tam = chuso[a[i, 2]] + " trăm " + tam;
                    }
                if (tam.Trim() != "")
                {
                    tienchu = tienchu + " " + tam + " " + donvi[i];
                }
                else
                {
                    tienchu = tienchu + " " + tam + " ";
                }
                tam = "";
            }

            return tienchu + " đồng";
        }
        private void txtThanhtoan_EditValueChanged(object sender, EventArgs e)
        {
            this.txtConno.EditValue = Convert.ToDecimal(this.hoadon.conno - Convert.ToDouble(this.txtThanhtoan.EditValue.ToString().Trim()));
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }
    }
}